package com.colorful.insuranceproject.claims;

import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.base.TitleActivity;
import com.colorful.insuranceproject.bean.ClaimsDetailsBean;
import com.colorful.insuranceproject.consts.ClaimsStatus;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.request.ClaimsRequest;
import com.lovely3x.common.utils.Response;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by handsome-Bob on 2017/8/18.
 * <p>
 * 理赔详情页
 */
public class ClaimsDetailsActivity extends TitleActivity {

    ClaimsRequest claimsRequest;

    public static final String EXTRA_CLAIMS_ORDER_ID = "extra_claims_order_id";

    private String id;

    private static final int GET_CLAIMS_DETAILS_URL = 0X1;

    /**
     * 理赔状态
     */
    @Bind(R.id.tv_claims_status)
    TextView tvClaimsStatus;

    /**
     * 理赔单号
     */
    @Bind(R.id.tv_claims_order_num)
    TextView tvClaimsOrderNum;

    /**
     * 理赔票据数量
     */
    @Bind(R.id.tv_claims_bill_num)
    TextView tvClaimsBillNum;

    /**
     * 理赔金额
     */
    @Bind(R.id.tv_application_amount)
    TextView tvApplicationAmount;

    /**
     * 统筹金额
     */
    @Bind(R.id.tv_overall_amount)
    TextView tvOverallAmount;

    /**
     * 实际赔付金额
     */
    @Bind(R.id.tv_actual_claim_amount)
    TextView tvActualClaimAmount;

    /**
     * 赔付比例
     */
    @Bind(R.id.tv_payout_ratio)
    TextView tvPayoutRatio;

    /**
     * 保险类型
     */
    @Bind(R.id.tv_insurance_type)
    TextView tvInsuranceType;

    /**
     * 理赔人员
     */
    @Bind(R.id.tv_claims_person)
    TextView tvClaimsPerson;

    /**
     * 创建时间
     */
    @Bind(R.id.tv_create_time)
    TextView tv_create_time;

    /**
     * 理赔时间
     */
    @Bind(R.id.tv_claims_time)
    TextView tvClaimsTime;

    /**
     * 备注
     */
    @Bind(R.id.tv_Remarks)
    TextView tvRemarks;

    /**
     * 理赔类型
     */
    @Bind(R.id.tv_application_style)
    TextView tvApplicationStyle;

    private ClaimsDetailsBean bean;

    @Override
    protected int getContentView() {
        return R.layout.activity_claims_details;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setTitle(getString(R.string.claims_details));
    }

    @Override
    protected void onInitExtras(@NonNull Bundle bundle) {
        super.onInitExtras(bundle);
        id = bundle.getString(EXTRA_CLAIMS_ORDER_ID);
    }

    @Override
    protected void onViewInitialized() {
        claimsRequest = new ClaimsRequest(getHandler());
        claimsRequest.getClaimsDetails(id, GET_CLAIMS_DETAILS_URL);
    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        switch (msg.what) {
            case GET_CLAIMS_DETAILS_URL:
                if (response.isSuccessful) {
                    setPageData((ClaimsDetailsBean) response.obj);
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;
        }
    }

    /**
     * 设置页面数据
     */
    private void setPageData(ClaimsDetailsBean obj) {
        bean = obj;
        tvClaimsStatus.setText(ClaimsStatus.judgeCaimsSatus(obj.getInsuranceStatus()));//理赔状态
        tvClaimsOrderNum.setText(obj.getInsuranceNo());//理赔单号
        tvClaimsBillNum.setText(obj.getPaperCount());//理赔票据数量
        tvApplicationAmount.setText(obj.getApplyPrice());//理赔金额
        tvOverallAmount.setText(obj.getWholePrice() != null && !obj.getWholePrice().equals("") ? obj.getWholePrice() : "-");//统筹金额
        tvActualClaimAmount.setText(obj.getRealPrice() != null && !obj.getRealPrice().equals("") ? obj.getRealPrice() : "-");//实际赔付金额
        tvPayoutRatio.setText(obj.getInsuranceProportion() != null && !obj.getInsuranceProportion().equals("") ? obj.getInsuranceProportion() : "-");//赔付比例
        tvInsuranceType.setText(obj.getInsuranceType());//保险类型
        tvClaimsPerson.setText(obj.getInsuranceName());//理赔人员
        tv_create_time.setText(obj.getCreateTime());//创建时间
        tvClaimsTime.setText(obj.getInsuranceTime());//理赔时间
        tvRemarks.setText(obj.getApplyPrice());//核对金额
        tvApplicationStyle.setText(obj.getInsuranceType());//理赔类型
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    @OnClick(R.id.tv_search)
    void onClicked() {
        showHTTPURLImgOnNewActivity(bean.getPaperList());
    }
}
